package com.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

public class LSUtil {

	static String b2 = "ywJKImoPxuM28nlTYBNb5A3hFe1DSZdi07jpRO9tWQLUEzXcasCHvGrgf6qVk4";
	static String a1 = "pBAaMxg8fk9L2iuY0JhZU5RoHQtwvOG4jT1dq6Dl3C7zbrnesVNIcFmXSEPKyW";
	static String e5 = "38toAIuYv6aMNKBix2ncsTmXPjShR9dUpECJ4FykgebrZLHDfQ0W7VGz5wOl1q";
	static String d4 = "eTqsOax4EG0vb3dLyNIUnAhzPp6fKZXrJwFkBMDS5uYgWjQo97ct81RmCVHil2";
	static String c3 = "vdrpAqUl9fbN4QB36CztocDnxMZXWgLEkmhJsyV7ujO0IPSiH2R5aw1GFYe8TK";
	static String f6 = "cHj65nksL7FuUpvZKaMIRlDyAiV4PQmYebN83JgdwSTC9oOzrqtXG1Ef0WhxB2";

	// 鍔犲瘑
	public static String encrypt(String p,String key) {
		String v = "";
		if (null == p || "".equals(p)) {
			return v;
		}
		for (int i = 0; i < p.length(); i++) {
			String a = p.substring(i, i + 1);
			int w = i % 6;
			w = w + 1;
			String o = "";
			if (w == 1) {
				o = a1;
			} else if (w == 2) {
				o = b2;
			} else if (w == 3) {
				o = c3;
			} else if (w == 4) {
				o = d4;
			} else if (w == 5) {
				o =  e5;
			} else if (w == 6) {
				o =  f6;
			}
			int indexof = o.indexOf(a);
			if (indexof != -1) {
				if (indexof + 1 != o.length()) {
					v = v + o.substring(indexof + 1, indexof + 2);
				} else {
					v = v + o.substring(0, 1);
				}

			} else {
				v = v + a;
			}
		}
		if (null != key) {
//			!key.equals("") || 
			v = getString(v, key, 0);
		}
		return v;
	}

	// 瑙ｅ瘑
	public static String decrypt(String p,String key) {
		String v = "";
		if (null == p || "".equals(p)) {
			return v;
		}
//		if (!key.equals("") || null != key) {
//			p = getString(p, key, 1);
//		}
		for (int i = 0; i < p.length(); i++) {
			String a = p.substring(i, i+1);
			int w = i%6;
			w=w+1;
			String o = "";
			if(w==1){
				o = a1;
			}else if(w==2){
				o = b2;
			}else if(w==3){
				o = c3;
			}else if(w==4){
				o = d4;
			}else if(w==5){
				o = e5;
			}else if(w==6){
				o = f6;
			}
			int indexof = o.indexOf(a);
			if (indexof != -1) {
				if (indexof != 0) {
					v = v + o.substring(indexof - 1, indexof);
				} else {
					v = v + o.substring(o.length() - 1, o.length());
				}

			} else {
				v = v + a;
			}
		}
		return v;
	}

	public static String getString(String pass, String key, Integer type) {
		if (type == 0) {
			return DecryptDes.encrypt(pass, key);
		} else {
			try {
				return DecryptDes.decrypt(pass, key);
			} catch (Exception e) {
				e.printStackTrace();
				return "";
			}
		}
	}
}
// public static void main(String[] args) {
// System.out.println(encrypt("123456789"));
// System.out.println(decrypt("7JWvIc_Jn"));

// net.sf.json.JSONObject result = new net.sf.json.JSONObject();
// JSONObject s = new JSONObject();
// s.put("ecode", "000002");
// String result1 = httpRequest(
// "https://jifen-integralcenterser.v5q.cn/getRsaKey", "POST", s.toString(),
// null, null);
// System.out.println(">>>"+result1);

// public static String httpRequest(String requestUrl,
// String requestMethod, String outputStr, String key, String ecode) {
// String str1 = null;
// StringBuffer buffer = new StringBuffer();
// try {
// // 鍒涘缓SSLContext瀵硅薄锛屽苟浣跨敤鎴戜滑鎸囧畾鐨勪俊浠荤鐞嗗櫒鍒濆鍖�
// TrustManager[] tm = { new MyX509TrustManager() };
// SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
// sslContext.init(null, tm, new java.security.SecureRandom());
// // 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
// SSLSocketFactory ssf = sslContext.getSocketFactory();
//
// URL url = new URL(requestUrl);
// HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
// .openConnection();
// httpUrlConn.setSSLSocketFactory(ssf);
// // httpUrlConn.addRequestProperty("k", key);
// // httpUrlConn.addRequestProperty("ecode", ecode);
// httpUrlConn.setDoOutput(true);
// httpUrlConn.setDoInput(true);
// httpUrlConn.setUseCaches(false);
// // 璁剧疆璇锋眰鏂瑰紡锛圙ET/POST锛�
// httpUrlConn.setRequestMethod(requestMethod);
//
// if ("GET".equalsIgnoreCase(requestMethod))
// httpUrlConn.connect();
//
// // 褰撴湁鏁版嵁闇�鎻愪氦鏃�
// if (null != outputStr) {
// OutputStream outputStream = httpUrlConn.getOutputStream();
// // 娉ㄦ剰缂栫爜鏍煎紡锛岄槻姝腑鏂囦贡鐮�
// outputStream.write(outputStr.getBytes("UTF-8"));
// outputStream.close();
// }
// // System.out.println(httpUrlConn.getResponseCode());
//
// // 灏嗚繑鍥炵殑杈撳叆娴佽浆鎹㈡垚瀛楃涓�
// InputStream inputStream = httpUrlConn.getInputStream();
// InputStreamReader inputStreamReader = new InputStreamReader(
// inputStream, "utf-8");
// BufferedReader bufferedReader = new BufferedReader(
// inputStreamReader);
//
// String str = null;
// while ((str = bufferedReader.readLine()) != null) {
// buffer.append(str);
// }
// // System.out.println("outputStr:" + buffer.toString());
// bufferedReader.close();
// inputStreamReader.close();
// // 閲婃斁璧勬簮
// inputStream.close();
// inputStream = null;
// httpUrlConn.disconnect();
// str1 = buffer.toString();
// } catch (ConnectException ce) {
// ce.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return str1;
// }

// }
